<template>
	<view class="page-main">
		<liu-chooseAddress ref="liuChooseAddress" @submit="submitAddress" :shopAddress="shopAddress"
			@detele="deteleAddress">
		</liu-chooseAddress>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressList: {},
				addressId: '',
				shopAddress: '',



			}
		},
		onLoad(options) {
			// this.type = options["type"];
			// this.addressId = options["addressId"];

		},
		mounted() {
			this.userId = uni.getStorageSync('userId');
			const query = this.$route.query;
			const action = query.action;
			this.addressId = query.addressId;
			this.shopAddress = query.shopAddress
			console.log('action参数：', action);
			console.log('shopAddress：', this.shopAddress);

		},

		methods: {
			handleClick() {
				const action = this.$route.query.action;
				const id = this.$route.query.id;

				if (action === 'add') {
					// 执行添加地址的请求
					// 可以根据需要使用uni.request或uni.$http等方法发送请求
					console.log('添加地址', this.addressList)

					let requestData = {
						name: this.addressList.userName,
						phone: this.addressList.userPhone,
						address: this.addressList.address,
						detailAddress: this.addressList.detailAddress,
						userId: this.userId,
						latitude: this.addressList.latitude,
						longitude: this.addressList.longitude
					};
					console.log('用户ID', this.userId)
					uni.request({
						url: 'http://43.136.172.73/api/v1/address/add',
						method: 'POST',
						data: requestData, // 直接使用requestData作为请求参数
						header: {
							// "Content-Type": "application/json",
							'Authorization': 'sfsdf',
						},
						success: (res) => {
							// 请求成功的回调函数
							// 假设返回的数据是一个数组
							console.log('请求成功', res)
							uni.showToast({
								title: '新增地址成功',
								icon: 'none'
							})
						},
						fail: function(err) {
							// 请求失败的回调函数
							console.log('请求失败', err)
							uni.showToast({
								title: '登录失败',
								icon: 'none'
							})
						}
					});



				} else if (action === 'edit') {
					// 执行编辑地址的请求，使用id参数进行操作
					// 可以根据需要使用uni.request或uni.$http等方法发送请求
					// 执行添加地址的请求
					// 可以根据需要使用uni.request或uni.$http等方法发送请求
					console.log('编辑地址', this.addressList)

					let requestData = {
						name: this.addressList.userName,
						phone: this.addressList.userPhone,
						id: this.addressId,
						address: this.addressList.address,
						detailAddress: this.addressList.detailAddress,
						latitude: this.addressList.latitude,
						longitude: this.addressList.longitude


					};
					console.log('requestData', requestData)
					uni.request({
						url: 'http://43.136.172.73/api/v1/address/update',
						method: 'PUT',
						data: requestData, // 直接使用requestData作为请求参数
						header: {
							// "Content-Type": "application/x-www-form-urlencoded",
							'Authorization': 'sfsdf',


						},
						success: (res) => {
							// 请求成功的回调函数
							// 假设返回的数据是一个数组
							console.log('修改请求成功', res)
							uni.showToast({
								title: '修改地址成功',
								icon: 'none'
							})
						},
						fail: function(err) {
							// 请求失败的回调函数
							console.log('修改失败', err)
							uni.showToast({
								title: '登录失败',
								icon: 'none'
							})
						}
					});

				}
			},
			//添加地址成功回调
			submitAddress(e) {
				this.addressList = e
				console.log('添加地址信息：', e)
				this.handleClick()
			},
			//删除地址回调
			deteleAddress(e) {
				console.log('所删除的地址信息：', e)
			},
			// 地址回显(编辑)调用此方法
			editAddress() {
				let obj = {
					id: '1',
					detailAddress: '', //门牌号(详细地址)
					userName: '', //联系人姓名
					userPhone: '', //手机号
					lableName: '', //标签
					sexType: '', //性别
					title: '', //地址概述
					address: '', //详细地址
					longitude: '', //经度
					latitude: '', //纬度
					agreeState: '', //是否是默认地址
				}
				this.$refs.liuChooseAddress.setData(obj)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page-main {
		width: 100%;
		height: 100vh;
	}
</style>